#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int m_max = 2e6 + 10;
const int n_max = 1e5 + 10;

struct edge{
    int x, y, pre;
}e[m_max];
int elen = 0, last[n_max];

// 插入边的函数
void ins(int x, int y) {
    e[++elen] = {x, y, last[x]};
    last[x] = elen;
}

bool vis_dfs[n_max];  // DFS 访问标记数组
bool vis_bfs[n_max];  // BFS 访问标记数组

// 深度优先搜索函数
void dfs(int x) {
    cout << x << " ";
    vis_dfs[x] = true;
    vector<int> next_nodes;
    // 收集当前节点的所有邻接节点
    for (int i = last[x]; i; i = e[i].pre) {
        next_nodes.push_back(e[i].y);
    }
    // 对邻接节点进行排序
    sort(next_nodes.begin(), next_nodes.end());
    for (int next : next_nodes) {
        if (!vis_dfs[next]) {
            dfs(next);
        }
    }
}

// 广度优先搜索函数
void bfs(int x) {
    queue<int> q;
    q.push(x);
    vis_bfs[x] = true;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        cout << u << " ";
        vector<int> next_nodes;
        // 修正：使用 u 来收集当前节点的所有邻接节点
        for (int i = last[u]; i; i = e[i].pre) {
            next_nodes.push_back(e[i].y);
        }
        // 对邻接节点进行排序
        sort(next_nodes.begin(), next_nodes.end());
        for (int next : next_nodes) {
            if (!vis_bfs[next]) {
                q.push(next);
                vis_bfs[next] = true;
            }
        }
    }
}

int main() {
    int n, m, x, y;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        ins(x, y);
    }

    dfs(1);
    cout << endl;
    bfs(1);
    
    return 0;
}